<template>
  <Form
    class="xtx-form"
    ref="formCom"
    :validation-schema="mySchema"
    v-slot="{ errors }"
  >
    <div class="xtx-form-item">
      <div class="field">
        <i class="icon iconfont icon-user"></i>
        <Field
          class="input"
          :class="{ err: errors.account }"
          name="account"
          type="text"
          v-model="form.account"
          placeholder="请输入用户名"
        />
      </div>
      <div class="error" v-if="errors.account">{{ errors.account }}</div>
    </div>
    <div class="xtx-form-item">
      <div class="field">
        <i class="icon iconfont icon-phone"></i>
        <Field
          class="input"
          :class="{ err: errors.mobile }"
          v-model="form.mobile"
          name="mobile"
          type="text"
          placeholder="请输入手机号"
        />
      </div>
      <div class="error" v-if="errors.mobile">{{ errors.mobile }}</div>
    </div>
    <div class="xtx-form-item">
      <div class="field">
        <i class="icon iconfont icon-code"></i>
        <Field
          class="input"
          :class="{ err: errors.code }"
          v-model="form.code"
          name="code"
          type="text"
          placeholder="请输入验证码"
        />
        <span class="code" @click="send()">{{
          time === 0 ? '发送验证码' : `${time}秒后发送`
        }}</span>
      </div>
      <div class="error" v-if="errors.code">{{ errors.code }}</div>
    </div>
    <div class="xtx-form-item">
      <div class="field">
        <i class="icon iconfont icon-lock"></i>
        <Field
          class="input"
          :class="{ err: errors.password }"
          v-model="form.password"
          name="password"
          type="password"
          placeholder="请输入密码"
        />
      </div>
      <div class="error" v-if="errors.password">{{ errors.password }}</div>
    </div>
    <div class="xtx-form-item">
      <div class="field">
        <i class="icon iconfont icon-lock"></i>
        <Field
          class="input"
          :class="{ err: errors.rePassword }"
          v-model="form.rePassword"
          name="rePassword"
          type="password"
          placeholder="请确认密码"
        />
      </div>
      <div class="error" v-if="errors.rePassword">{{ errors.rePassword }}</div>
    </div>
    <a href="javascript:;" class="submit" @click="submit()">立即提交</a>
  </Form>
</template>

<script>
import schema from '@/utils/vee-validate-schema'
import { Form, Field } from 'vee-validate'
import { reactive, getCurrentInstance, ref, onUnmounted } from 'vue'
import { userQQPatchCode, userQQPatchLogin } from '@/apis/user'
import { useStore } from 'vuex'
import { useRouter } from 'vue-router'
export default {
  name: 'CallbackPatch',
  components: { Form, Field },
  setup (props) {
    // 表单数据对象
    const form = reactive({
      account: null,
      mobile: null,
      code: null,
      password: null,
      rePassword: null
    })
    // 表单校验规则
    const mySchema = {
      account: schema.accountApi,
      mobile: schema.mobile,
      code: schema.code,
      password: schema.password,
      rePassword: schema.rePassword
    }

    // 发送手机号
    // 定时器
    let timer = null
    const { proxy } = getCurrentInstance()
    const formCom = ref(null)
    onUnmounted(() => {
      clearInterval(timer)
    })
    const time = ref(0)
    const send = async () => {
      const ismobile = schema.mobile(form.mobile)
      if (ismobile === true) {
        if (time.value === 0) {
          await userQQPatchCode(form.mobile)
          proxy.$message({ type: 'success', text: '发送成功' })
          time.value = 60
          timer = setInterval(() => {
            time.value--
            if (time.value <= 0) {
              clearInterval(timer)
            }
          }, 1000)
        }
      } else {
        formCom.value.setFieldError('mobile', ismobile)
      }
    }

    // 完善信息
    const store = useStore()
    const router = useRouter()
    const submit = async () => {
      const valid = formCom.value.validate()
      if (valid) {
        userQQPatchLogin({
          unionId: props.unionId,
          ...form
        })
          .then((data) => {
            // 实现和之前登录一样的逻辑
            const { id, account, avatar, mobile, nickname, token } = data.result
            store.commit('user/setUser', {
              id,
              account,
              avatar,
              mobile,
              nickname,
              token
            })
            store.dispatch('cart/mergeCart').then(() => {
              router.push(store.state.user.redirectUrl)
              proxy.$message({ type: 'success', text: '注册账号成功' })
            })
            // store.dispatch('cart/mergeCart').then(() => {
            //   // 2. 跳转到来源页或者首页
            //   router.push(store.state.user.redirectUrl)
            //   // 3. 成功提示
            //   proxy.$message({ type: 'success', text: 'QQ完善信息成功' })
            // })
          })
          .catch((e) => {
            console.log(e)
            proxy.$message({ type: 'error', text: '完善信息失败' })
          })
      }
    }
    return { form, mySchema, send, formCom, time, submit }
  }
}
</script>

<style scoped lang="less">
.code {
  position: absolute;
  right: 0;
  top: 0;
  line-height: 50px;
  width: 80px;
  color: #999;
  &:hover {
    cursor: pointer;
  }
}
</style>
